package com.example.hotelsystem.controller.check;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.vo.checkin.AutoCheckinVO;
import com.example.hotelsystem.vo.checkout.AutoCheckoutVO;
import com.example.hotelsystem.vo.checkout.CheckOutVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/checkout")
@Api(tags = "退房相关接口控制器")
public class CheckoutController {
    @Autowired
    CheckoutService checkoutService;
    @Autowired
    HotelorderService hotelorderService;
    @Autowired
    RoomService roomService;
    @Autowired
    UserService userService;
    @Autowired
    HotelNameService hotelNameService;
    @Autowired
    CityService cityService;


    @ApiOperation("ocr自主退房")
    @PostMapping("/autoCheckout")
    public Result autoCheckin(AutoCheckoutVO autoCheckoutVO){
        try{
            Date date = new Date();
            Hotelorder order = hotelorderService.getById(autoCheckoutVO.getOrderId());
            if(date.getTime()-(order.getCheckoutTime().getTime()+86400000)>0){
                return Result.error("您未按期退房，请联系管理员进行处理");
            }
            QueryWrapper<User> idcardQuery = new QueryWrapper<>();
            idcardQuery.eq("idcard",autoCheckoutVO.getIdcard());
            User user = userService.getOne(idcardQuery);
            if(user == null ){
                return Result.error("身份证不匹配");
            }
            if(user.getId().equals(order.getUserId())){
                Checkout out = new Checkout();
                out.setOutTime(date);

                Room room = roomService.getById(order.getRoomId());
                out.setRoomNum(room.getNumber());
                out.setOperatorId(user.getId());
                out.setHotelId(room.getHotelId());
                out.setCityId(room.getCityId());
                boolean isOut = checkoutService.save(out);
                if(isOut){
                    String lock = String.valueOf((int)((Math.random()*9+1)*100000));
                    room.setLockPassword(lock);
                    room.setStatus(3);
                    roomService.updateById(room);

                    order.setStatus(2);
                    hotelorderService.updateById(order);
                    return Result.success("自主退房成功",lock);
                }
                return Result.error("自主退房失败");
            }else{
                return Result.error("身份证与订单不匹配");
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("checkout入住信息列表")
    @PostMapping("/list")
    public Result CheckoutList(@RequestParam(defaultValue = "1") Integer currentPage){
        Page page = new Page(currentPage, Pagesize.pageSize1);
        IPage<Checkout> checkoutList=checkoutService.page(page);
        if(checkoutList!=null){
            for(Checkout item :checkoutList.getRecords()){
                City c = cityService.getById(item.getCityId());
                HotelName n = hotelNameService.getById(item.getHotelId());
                item.setCityName(c.getCityName());
                item.setHotelName(n.getHotelName());
            }
            return Result.success(checkoutList);
        }
        return Result.error(CodeError.CHECKOUT_LIST_NULL,"获取值为空");
    }


    @ApiOperation("填写checkout退房登记信息接口")
    @PostMapping("/add")
    public Result AddCheckout(CheckOutVO vo){
        Checkout checkout = new Checkout();
        BeanUtils.copyProperties(vo,checkout);
        boolean isAdd = checkoutService.save(checkout);
        if(isAdd){
            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("number",checkout.getRoomNum());
            Room room = roomService.getOne(queryWrapper);
            if(room==null){
                return Result.error("房间不存在，请检查房间号是否输入错误");
            }
            room.setStatus(3);
            roomService.updateById(room);
            return Result.success("退房信息登记成功");
        }else{
            return Result.error(CodeError.CHECKOUT_FAIL,"登记失败,😭");
        }
    }

    @ApiOperation("修改checkout退房信息接口")
    @PostMapping("/mod")
    public Result ModifyCheckoutInfo(Checkout checkout){
        boolean isUpdate = checkoutService.updateById(checkout);
        if(isUpdate){
            return Result.success("修改成功");
        }else{
            return Result.error(CodeError.CHECKOUT_MOD_FAIL,"修改失败,😭");
        }
    }

}

